package com.example.onlinemusic.controller;

import com.example.onlinemusic.mapper.UserMapper;
import com.example.onlinemusic.model.User;
import com.example.onlinemusic.tools.Constant;
import com.example.onlinemusic.tools.ResponseBodyMessage;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import lombok.SneakyThrows;
import org.apache.coyote.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {
    /*//这一段好像都是引入自动生成/添加的，先注释掉，老师的代码没有这些
    //private final UserMapper userMapper;

    public UserController(UserMapper userMapper) {
        this.userMapper = userMapper;
    }*/
    @Autowired
    private UserMapper userMapper;//果然，和上边注释掉的东西有点类似

    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;


    @RequestMapping("/login1")
    public ResponseBodyMessage<User> login1(@RequestParam String username, @RequestParam String password,
                                           HttpServletRequest request) {
        User userLogin = new User();


        userLogin.setUsername(username);
        userLogin.setPassword(password);

        User user = userMapper.login(userLogin);

        if(user != null){
            System.out.println("登录成功");//——控制台打印
             //可以优化：登录成功之后，可以把当前的用户写到session里边
            request.getSession().setAttribute("balbala",user);
            return new ResponseBodyMessage<User>(0,"登录成功",userLogin);
        }else{
            System.out.println("登录失败");
            return new ResponseBodyMessage<User>(-1,"登录失败",userLogin);
        }

    }


    @SneakyThrows
    @RequestMapping("/login")
    public ResponseBodyMessage<User> login(@RequestParam String username, @RequestParam String password,
                                           HttpServletRequest request, HttpServletResponse response) {
       /* User userLogin = new User();


        userLogin.setUsername(username);
        userLogin.setPassword(password);

        User user = userMapper.login(userLogin);*/

        System.out.println("调试：/user/login");
        User user = userMapper.selectByName(username);
        if(user == null){
            System.out.println("登录失败 ");//——控制台打印


            return new ResponseBodyMessage<User>(-1,"用户名或密码错误111！",user);

        }else{
            boolean flg = bCryptPasswordEncoder.matches(password, user.getPassword());
            if(!flg){
                return new ResponseBodyMessage<User>(-1,"用户名或密码错误222！",user);
            }
            //可以优化：登录成功之后，可以把当前的用户写到session里边
            request.getSession().setAttribute(Constant.USERINFO_SESSION_KEY,user);
            System.out.println("登录成功");
            //我觉得哈：这里也要页面跳转
            //response.sendRedirect("/list.html");
            return new ResponseBodyMessage<User>(0,"登录成功",user);

        }

    }
}
